package com.ikun.aimian.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ikun.aimian.common.BaseResponse;
import com.ikun.aimian.common.ErrorCode;
import com.ikun.aimian.common.ResultUtils;
import com.ikun.aimian.exception.BusinessException;
import com.ikun.aimian.exception.ThrowUtils;
import com.ikun.aimian.model.dto.userfavour.FavoriteDeleteRequest;
import com.ikun.aimian.model.dto.userfavour.UserFavoriteAddRequest;
import com.ikun.aimian.model.entity.UserFavorite;
import com.ikun.aimian.model.vo.UserFavoriteVO;
import com.ikun.aimian.service.UserFavoriteService;
import com.ikun.aimian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/favorite")
@Slf4j
public class UserFavoriteController {

    @Resource
    private UserFavoriteService userFavoriteService;

    @Resource
    private UserService userService;

    /**
     * 用户收藏应用
     */
    @PostMapping("/add")
    public BaseResponse<Long> addFavorite(@RequestBody UserFavoriteAddRequest req, HttpServletRequest request) {
        ThrowUtils.throwIf(req == null, ErrorCode.PARAMS_ERROR);
        if (req.getAppId() == null || req.getAppId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "应用ID不能为空");
        }
        Long userId = userService.getLoginUser(request).getId();
        UserFavorite favorite = new UserFavorite();
        favorite.setAppId(req.getAppId());
        favorite.setUserId(userId);
        userFavoriteService.validUserFavorite(favorite, true);
        boolean result = userFavoriteService.save(favorite);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "收藏失败");
        return ResultUtils.success(favorite.getId());
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteFavorite(@RequestBody FavoriteDeleteRequest deleteRequest,
                                                HttpServletRequest request) {
        // 参数校验
        if (deleteRequest == null || deleteRequest.getAppId() == null || deleteRequest.getAppId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数错误");
        }
        // 获取当前登录用户
        Long userId = userService.getLoginUser(request).getId();
        Long appId = deleteRequest.getAppId();

        // 使用 UpdateWrapper 来指定更新字段和 WHERE 条件
        UpdateWrapper<UserFavorite> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("userId", userId)
                .eq("appId", appId)
                .eq("isDelete", 0)
                .set("isDelete", 1);

        boolean result = userFavoriteService.update(null, updateWrapper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "取消收藏失败");
        return ResultUtils.success(true);
    }


    /**
     * 我的收藏列表
     */
    @GetMapping("/list")
    public BaseResponse<List<UserFavoriteVO>> listFavorites(HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        List<UserFavoriteVO> list = userFavoriteService.getUserFavoriteVOListByUserId(userId);
        return ResultUtils.success(list);
    }
}
